#!/usr/bin/env python
# -*- coding:utf-8 -*-

from tornado_extend import HTTPServer, HTTPWorker, Master
from tornado.web import RequestHandler
from tornado import gen
import time
import json
from dm_py_util import DEBUG_LOG, WARN_LOG, ERROR_LOG, SET_LOG_KEY, BUSINESS_LOG
from ConfigParser import ConfigParser
from inke_replace_ads_manager import inkeReplaceAdsManager
from inke_replace_ads_handler import inkeReplaceAdsHandler, inkeReplaceAdsHandler1, CheckRepeatMessageHandler, inkeContentAdsHandler


class inkeReplaceAdsServer(HTTPServer):
    #TODO: 此处注册业务Handler

    def install_handler(self):
        return [inkeReplaceAdsHandler, inkeReplaceAdsHandler1, CheckRepeatMessageHandler, inkeContentAdsHandler]


class inkeReplaceAdsWorker(HTTPWorker):

    def init_worker(self, config):
        super(inkeReplaceAdsWorker, self).init_worker(config)
        from tornado_extend.extend import set_timeout, set_interval
        try:
            cfg = ConfigParser()
            cfg.read(config)
        except Exception as e:
            ERROR_LOG('int worker failed', e)
            exit(1)
        try:
            inke_replace_ads_manager_config = cfg.get(
                'inke_replace_ads_manager', 'config')
            inkeReplaceAdsManager().init(inke_replace_ads_manager_config)
        except Exception as e:
            ERROR_LOG('init manager error', e)
            exit(1)
        DEBUG_LOG('init manager success...')

if __name__ == '__main__':
    s = inkeReplaceAdsServer(
        main_config='/a8root/conf/inke_replace_ads/master.config', worker=inkeReplaceAdsWorker)
    DEBUG_LOG('inkeReplaceAdsServer start...')
    s.run()
